﻿<%@ Page Language="C#" MasterPageFile="../../main.master" Inherits="VioletDevelopmentLaboratory.Net.Web.PageBase" %>
<%@ Import Namespace="System.Data" %>

<%@ Register src="modify.ascx" tagname="modify" tagprefix="uc1" %>

<script runat="server">
	private Board currentBoard;

	protected void Page_Load(Object sender, EventArgs e)
	{
		if (Request.UrlReferrer == null)
		{
			Response.Redirect("list.aspx");
		}

		currentBoard = Board.GetBoard(Request.QueryString["id"]);

		if (!Page.IsPostBack)
		{
			RolesListBound();

			BoardInfoBind();
		}
	}

	private void RolesListBound()
	{
		Hashtable addGuestRolesHashtable = new Hashtable();
		addGuestRolesHashtable.Add("게스트(모든사용자)", DefaultRoles.Guest);
		addGuestRolesHashtable.Add("인증된사용자(모든역할포함)", DefaultRoles.Authenticated);

		Hashtable rolesHashtable = new Hashtable();
		rolesHashtable.Add("인증된사용자(모든역할포함)", DefaultRoles.Authenticated);

		if (Roles.Enabled)
		{
			String[] roles = Roles.GetAllRoles();

			foreach (String role in roles)
			{
				addGuestRolesHashtable.Add(role, role);
			}

			foreach (String role in roles)
			{
				rolesHashtable.Add(role, role);
			}
		}

		cbl_ListView.DataSource = addGuestRolesHashtable;
		cbl_ListView.DataTextField = "key";
		cbl_ListView.DataValueField = "value";
		cbl_ListView.DataBind();

		cbl_AnnoWrite.DataSource = rolesHashtable;
		cbl_AnnoWrite.DataTextField = "key";
		cbl_AnnoWrite.DataValueField = "value";
		cbl_AnnoWrite.DataBind();

		cbl_ThreadWrite.DataSource = rolesHashtable;
		cbl_ThreadWrite.DataTextField = "key";
		cbl_ThreadWrite.DataValueField = "value";
		cbl_ThreadWrite.DataBind();

		cbl_ThreadReplyWrite.DataSource = rolesHashtable;
		cbl_ThreadReplyWrite.DataTextField = "key";
		cbl_ThreadReplyWrite.DataValueField = "value";
		cbl_ThreadReplyWrite.DataBind();

		cbl_CommentWrite.DataSource = rolesHashtable;
		cbl_CommentWrite.DataTextField = "key";
		cbl_CommentWrite.DataValueField = "value";
		cbl_CommentWrite.DataBind();

		cbl_Read.DataSource = addGuestRolesHashtable;
		cbl_Read.DataTextField = "key";
		cbl_Read.DataValueField = "value";
		cbl_Read.DataBind();

		cbl_FileUpload.DataSource = rolesHashtable;
		cbl_FileUpload.DataTextField = "key";
		cbl_FileUpload.DataValueField = "value";
		cbl_FileUpload.DataBind();

		cbl_FileDownload.DataSource = addGuestRolesHashtable;
		cbl_FileDownload.DataTextField = "key";
		cbl_FileDownload.DataValueField = "value";
		cbl_FileDownload.DataBind();
	}

	private string GetCheckedRoles(ListItemCollection items)
	{
		String _roles = "";

		foreach (ListItem item in items)
		{
			if (item.Selected)
			{
				_roles += item.Value + "\r\n";
			}
		}

		return _roles;
	}

	private void SetCheckingRoles(ListItemCollection items, String[] roles)
	{
		foreach (String _role in roles)
		{
			if (items.FindByValue(_role) != null)
			{
				items.FindByValue(_role).Selected = true;
			}
		}
	}

	private void BoardInfoBind()
	{
		InsertTagTextBox.Text = String.Format("<VWeb:Board ID=\"{0}\" runat=\"server\" ComponentID=\"{1}\" />", currentBoard.ComponentID + "Board", currentBoard.ComponentID);
		txt_ComponentId.Text = currentBoard.ComponentID;
		txt_Name.Text = currentBoard.Name;
		PageUrlTextBox.Text = currentBoard.PageUrl;
		cb_UseComment.Checked = currentBoard.UseComment;
		cb_UseReply.Checked = currentBoard.UseReply;
		cb_UseStressToday.Checked = currentBoard.UseStressToday;
		cb_UseStressNew.Checked = currentBoard.UseStressNew;
		cb_UseStressHot.Checked = currentBoard.UseStressHot;
		cb_UseVote.Checked = currentBoard.UseVote;

		StressHotLimitTextBox.Text = currentBoard.HotCount.ToString();
		cb_UseListView.Checked = currentBoard.UseListView;
		txt_ThreadLimit.Text = currentBoard.ThreadLimit.ToString();
		cb_UseFileUpload.Checked = currentBoard.UseFileUpload;
		FileUploadLimitTextBox.Text = currentBoard.FileUploadLimit.ToString();
		FileUploadSizeLimitTextBox.Text = currentBoard.FileUploadSizeLimit.ToString();
		txt_FileUploadExtension.Text = currentBoard.FileUploadExtension;
		ThumbnailWidthTextBox.Text = currentBoard.ThumbnailWidth.ToString();
		ThumbnailHeightTextBox.Text = currentBoard.ThumbnailHeight.ToString();
		ThumbnailSourceAspectRatio.Checked = currentBoard.ThumbnailSourceAspectRatio;
		PageSizeTextBox.Text = currentBoard.PageSize.ToString();
		PageButtonCountTextBox.Text = currentBoard.PageButtonCount.ToString();

		SetCheckingRoles(cbl_ListView.Items, currentBoard.ListViewRoles.Split(new String[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries));
		SetCheckingRoles(cbl_AnnoWrite.Items, currentBoard.AnnoWriteRoles.Split(new String[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries));
		SetCheckingRoles(cbl_ThreadWrite.Items, currentBoard.ThreadWriteRoles.Split(new String[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries));
		SetCheckingRoles(cbl_ThreadReplyWrite.Items, currentBoard.ThreadReplyWriteRoles.Split(new String[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries));
		SetCheckingRoles(cbl_CommentWrite.Items, currentBoard.CommentWriteRoles.Split(new String[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries));
		SetCheckingRoles(cbl_Read.Items, currentBoard.ReadRoles.Split(new String[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries));
		SetCheckingRoles(cbl_FileUpload.Items, currentBoard.FileUploadRoles.Split(new String[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries));
		SetCheckingRoles(cbl_FileDownload.Items, currentBoard.FileDownloadRoles.Split(new String[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries));

		RequiredCategoryCheckBox.Checked = currentBoard.RequiredCategory;
	}
	protected void ModifyButton_Click(Object sender, EventArgs e)
	{
		if (Page.IsValid)
		{
			BoardUpdateResult result = Board.Modify(
				txt_ComponentId.Text,
				txt_Name.Text,
				cb_UseComment.Checked,
				cb_UseReply.Checked,
				cb_UseFileUpload.Checked,
				cb_UseStressNew.Checked,
				cb_UseStressToday.Checked,
				cb_UseStressHot.Checked,
				cb_UseListView.Checked,
				cb_UseVote.Checked,
				Int32.Parse(StressHotLimitTextBox.Text),
				Int32.Parse(txt_ThreadLimit.Text),
				Int32.Parse(PageSizeTextBox.Text),
				Int32.Parse(PageButtonCountTextBox.Text),
				Int32.Parse(FileUploadLimitTextBox.Text),
				Int32.Parse(FileUploadSizeLimitTextBox.Text),
				txt_FileUploadExtension.Text,
				Int32.Parse(ThumbnailWidthTextBox.Text),
				Int32.Parse(ThumbnailHeightTextBox.Text),
				ThumbnailSourceAspectRatio.Checked,
				GetCheckedRoles(cbl_ListView.Items),
				GetCheckedRoles(cbl_AnnoWrite.Items),
				GetCheckedRoles(cbl_ThreadWrite.Items),
				GetCheckedRoles(cbl_ThreadReplyWrite.Items),
				GetCheckedRoles(cbl_Read.Items),
				GetCheckedRoles(cbl_CommentWrite.Items),
				GetCheckedRoles(cbl_FileUpload.Items),
				GetCheckedRoles(cbl_FileDownload.Items),
				RequiredCategoryCheckBox.Checked
			);

			switch (result)
			{
				case BoardUpdateResult.Success :
					VUtility.ShowAlert(Page, txt_ComponentId.Text + "이(가) 변경되었습니다.");
					break;
				default :
					VUtility.ShowAlert(Page, "존재하지 않는 게시판입니다.");
					break;
			}
			
		}
	}
</script>

<asp:Content ID="Content1" ContentPlaceHolderID="BodyContentPlaceHolder" runat="server">

	<h2>게시판 &gt; 목록 &gt; <%= String.Format("{0} ({1})", currentBoard.ComponentID, currentBoard.Name) %> &gt; 세부설정</h2>

	<uc1:modify ID="modify1" runat="server" />

	<fieldset>
		<legend>세부설정</legend>
		<div class="Field">
			<div class="FieldName">삽입태그</div>
			<div class="FieldValue"><asp:TextBox ID="InsertTagTextBox" runat="server" ReadOnly="true" Width="90%" /></div>
		</div>
		<div class="Field">
			<div class="FieldName">아이디</div>
			<div class="FieldValue"><asp:TextBox ID="txt_ComponentId" runat="server" Enabled="False" ReadOnly="True" /></div>
		</div>
		<div class="Field">
			<div class="FieldName">페이지 URL</div>
			<div class="FieldValue">
				<asp:TextBox ID="PageUrlTextBox" runat="server" Enabled="false" ReadOnly="true" />
				(게시판컨트롤이 삽입된 페이지 로드시에 자동 설정 또는 업데이트됩니다.)
			</div>
		</div>
		<div class="Field">
			<div class="FieldName">이름</div>
			<div class="FieldValue">
				<asp:TextBox ID="txt_Name" runat="server" />
				<asp:RequiredFieldValidator ID="rfv_txt_Name" runat="server" ControlToValidate="txt_Name" Display="Dynamic" ErrorMessage="*" />
			</div>
		</div>
		<div class="Field">
			<div class="FieldName">목록설정</div>
			<div class="FieldValue">
				목록글수 <asp:TextBox ID="PageSizeTextBox" runat="server" Columns="3" MaxLength="3" /> |
				페이징버튼수 <asp:TextBox ID="PageButtonCountTextBox" runat="server" Columns="3" MaxLength="3" /> |
				<asp:CheckBox ID="cb_UseListView" runat="server" Text="글보기에서 목록출력" />
			</div>
		</div>
		<div class="Field">
			<div class="FieldName">글수제한</div>
			<div class="FieldValue"><asp:TextBox ID="txt_ThreadLimit" runat="server" Text="-1" /> (-1은 무제한)</div>
		</div>
		<div class="Field">
			<div class="FieldName">옵션</div>
			<div class="FieldValue">
				<div>
					<asp:CheckBox ID="cb_UseReply" runat="server" Text="답글" /> |
					<asp:CheckBox ID="cb_UseComment" runat="server" Text="댓글" /> |
					<asp:CheckBox ID="cb_UseVote" runat="server" Text="투표" />
				</div>
				<div>
					<asp:CheckBox ID="cb_UseStressToday" runat="server" Text="오늘글강조" /> |
					<asp:CheckBox ID="cb_UseStressNew" runat="server" Text="새글(24시간내)강조" /> |
					<asp:CheckBox ID="cb_UseStressHot" runat="server" Text="인기글강조" />
					<asp:TextBox ID="StressHotLimitTextBox" runat="server" MaxLength="3" Columns="3" />Read이상
				</div>
				<div><asp:CheckBox ID="RequiredCategoryCheckBox" runat="server" Text="글작성시 카테고리를 필수항목으로 설정" /></div>
			</div>
		</div>
		<div class="Field">
			<div class="FieldName">파일첨부</div>
			<div class="FieldValue">
				<div><asp:CheckBox ID="cb_UseFileUpload" runat="server" Text="사용하기" /></div>
				<div>
					첨부개수 <asp:TextBox ID="FileUploadLimitTextBox" runat="server" Columns="3" MaxLength="3" />개 |
					최대파일크기 <asp:TextBox ID="FileUploadSizeLimitTextBox" runat="server" />KB
				</div>
				<div>첨부파일종류
					<asp:TextBox ID="txt_FileUploadExtension" runat="server" Width="40%" />
					<asp:RequiredFieldValidator ID="rfv_txt_FileUploadExtension" runat="server" ControlToValidate="txt_FileUploadExtension" Display="Dynamic" ErrorMessage="*" />
					(;세미콜론으로 구분, 예 : zip;jpg;gif)
				</div>
				<div>
					썸네일크기
					<asp:TextBox ID="ThumbnailWidthTextBox" runat="server" Width="100px" />
					<asp:RequiredFieldValidator ID="ThumbnailWidthTextBoxRequiredFieldValidator" runat="server" 
						ControlToValidate="ThumbnailWidthTextBox" Display="Dynamic" 
						ErrorMessage="[썸네일가로]를 입력하세요." SetFocusOnError="True">*</asp:RequiredFieldValidator>
					<asp:CompareValidator ID="ThumbnailWidthTextBoxCompareValidator" runat="server" 
						ControlToValidate="ThumbnailWidthTextBox" Display="Dynamic" 
						ErrorMessage="[썸네일가로]는 정수로 입력하세요." Operator="DataTypeCheck" 
						SetFocusOnError="True" Type="Integer">*</asp:CompareValidator>
					X
					<asp:TextBox ID="ThumbnailHeightTextBox" runat="server" Width="100px" />
					<asp:RequiredFieldValidator ID="ThumbnailHeightTextBoxRequiredFieldValidator" runat="server" 
						ControlToValidate="ThumbnailHeightTextBox" Display="Dynamic" 
						ErrorMessage="[썸네일세로]를 입력하세요." SetFocusOnError="True">*</asp:RequiredFieldValidator>
					<asp:CompareValidator ID="ThumbnailHeightTextBoxCompareValidator" 
						runat="server" ControlToValidate="ThumbnailHeightTextBox" Display="Dynamic" 
						ErrorMessage="[썸네일세로]는 정수로 입력하세요." Operator="DataTypeCheck" 
						SetFocusOnError="True" Type="Integer">*</asp:CompareValidator>
					<asp:CheckBox ID="ThumbnailSourceAspectRatio" runat="server" Text="원본비율유지하기" />
				</div>
			</div>
		</div>
		<div class="Field">
			<div class="FieldName">역할</div>
			<div class="FieldValue">
				<table border="0" cellspacing="0" cellpadding="5" width="100%">
					<tr>
						<td style="width : 90px;">목록보기</td>
						<td><asp:CheckBoxList ID="cbl_ListView" runat="server" CellPadding="3" 
								RepeatColumns="3" RepeatDirection="Horizontal" /></td>
					</tr>
					<tr>
						<td>공지사항쓰기</td>
						<td><asp:CheckBoxList ID="cbl_AnnoWrite" runat="server" CellPadding="3" 
								RepeatColumns="3" RepeatDirection="Horizontal" /></td>
					</tr>
					<tr>
						<td>글쓰기</td>
						<td><asp:CheckBoxList ID="cbl_ThreadWrite" runat="server" CellPadding="3" 
								RepeatColumns="3" RepeatDirection="Horizontal" /></td>
					</tr>
					<tr>
						<td>답변쓰기</td>
						<td><asp:CheckBoxList ID="cbl_ThreadReplyWrite" runat="server" CellPadding="3" 
								RepeatColumns="3" RepeatDirection="Horizontal" /></td>
					</tr>
					<tr>
						<td>댓글쓰기</td>
						<td><asp:CheckBoxList ID="cbl_CommentWrite" runat="server" CellPadding="3" 
								RepeatColumns="3" RepeatDirection="Horizontal" /></td>
					</tr>
					<tr>
						<td>글보기</td>
						<td><asp:CheckBoxList ID="cbl_Read" runat="server" CellPadding="3" 
								RepeatColumns="3" RepeatDirection="Horizontal" /></td>
					</tr>
					<tr>
						<td>파일첨부</td>
						<td><asp:CheckBoxList ID="cbl_FileUpload" runat="server" CellPadding="3" 
								RepeatColumns="3" RepeatDirection="Horizontal" /></td>
					</tr>
					<tr>
						<td>파일받기</td>
						<td><asp:CheckBoxList ID="cbl_FileDownload" runat="server" CellPadding="3" 
								RepeatColumns="3" RepeatDirection="Horizontal" /></td>
					</tr>
				</table>
			</div>
			<div class="Field"><asp:Button ID="ModifyButton" runat="server" Text="변경" OnClick="ModifyButton_Click" /></div>
		</div>
	</fieldset>
</asp:Content>